package com.itheima.work1114;

import java.util.Random;

//验证码
public class NumTik {
    public static void main(String[] args) {
        String cap = genera();
        System.out.println("验证码为：" + cap);
    }

    //设置验证码方法
    public static String genera() {
        //定义StringBuilder
        StringBuilder builder = new StringBuilder();
        for (char i = 'a'; i <= 'z'; i++) {
            builder.append(i);
        }
        for (char i = 'A'; i <= 'Z'; i++) {
            builder.append(i);
        }
        for (char i = '0'; i <= '9'; i++) {
            builder.append(i);
        }
        // 创建随机数对象
        Random r = new Random();
        // 定义一个空字符串用于保存验证码
        StringBuilder cap = new StringBuilder(4);
        //遍历随机索引输入
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(builder.length());
            cap.append(builder.charAt(index));
        }
        return cap.toString();
    }

}

